#include "documentdate.h"
#include <qdebug.h>

bool DocumentDate::isInit = false;

DocumentDate::DocumentDate()
{

}

DocumentDate::~DocumentDate()
{

}

bool DocumentDate::openSql()
{
    if(DocumentDate::isInit == true){
        return true;
    }
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
//    db.setHostName("");
//    db.setUserName("");
//    db.setPassword("");
//    db.setPort("");
    db.setDatabaseName("data.db");
    if(!db.open()){
        return false;
    }
    DocumentDate::isInit = true;
    return true;
}

bool DocumentDate::insertData()
{
    QString queryStr = "";
    QString contentTemp = content;
    contentTemp.replace("\n","CHAR(13)");
    queryStr.sprintf("insert into 'DocumentDate'('url','title','content') values('%s','%s','%s');",
                     url.toStdString().c_str(),title.toStdString().c_str(),contentTemp.toStdString().c_str());
    QSqlQuery query;
    qDebug()<<queryStr;
    return query.exec(queryStr);
}

bool DocumentDate::updateData()
{
    QString queryStr = "";
    queryStr.sprintf("update set 'title'='%s','content'='%s' from 'DocumentDate' where 'url'='%s';",
                     title.toStdString().c_str(),content.toStdString().c_str(),url.toStdString().c_str());
    QSqlQuery query;
    query.exec(queryStr);
    return true;
}

bool DocumentDate::isExistedData(QString url)
{
    QString queryStr = "";
    queryStr.sprintf("select * from 'DocumentDate'  where url='%s'",url.toStdString().c_str());
    QSqlQuery query;
    query.exec(queryStr);
    return query.next();
}

QList<DocumentDate> *DocumentDate::loadDataFromDatabase()
{
    QString queryStr = "";
    queryStr.sprintf("select id,url,title,content from DocumentDate;");
    QSqlQuery query;
    bool isTrue = query.exec(queryStr);
    if(isTrue){
        QList<DocumentDate> *list = new QList<DocumentDate>();
        while(query.next()){
            DocumentDate data;
            data.id = query.value(0).toInt();
            data.url = query.value(1).toString();
            data.title = query.value(2).toString();
            data.content = query.value(3).toString().replace("CHAR(13)","\n");
            list->push_back(data);
        }
        return list;
    }else{
        return NULL;
    }
}

QList<DocumentDate> DocumentDate::getData(QString url)
{
    QString queryStr = "";
    queryStr.sprintf("select id,url,title,content from DocumentDate where title = '%s';",
                     url.toStdString().c_str());
    QSqlQuery query;
    bool isTrue = query.exec(queryStr);
    QList<DocumentDate> list;
    if(isTrue){
        while(query.next()){
            DocumentDate data;
            data.id = query.value(0).toInt();
            data.url = query.value(1).toString();
            data.title = query.value(2).toString();
            data.content = query.value(3).toString().replace("CHAR(13)","\n");
            list.push_back(data);
        }
        return list;
    }else{
        return list;
    }
}

QList<DocumentDate> DocumentDate::getData(QString title, QString content)
{
  QString queryStr = "";
  queryStr.sprintf("select id,url,title,content from DocumentDate where ");
  if(title != NULL && title.length()!=0){
      QString titleStr = " title like '\%"+title+"\%'";
      queryStr += titleStr;
  }
  if(content != NULL && content.length() != 0){
      QString contentStr = "";
      if(title != NULL && title.length()!=0){
          contentStr += " or ";
      }
      contentStr += "content like '\%"+content+"\%'";
      queryStr += contentStr;
  }
  if(title.length() == 0 && content.length() == 0){
      queryStr = "select id,url,title,content from DocumentDate";
  }

  queryStr += ";";

  qDebug()<<"DocumentDate::getData :"<<queryStr;
  QSqlQuery query;
  bool isTrue = query.exec(queryStr);
  QList<DocumentDate> list;
  if(isTrue){
      while(query.next()){
          DocumentDate data;
          data.id = query.value(0).toInt();
          data.url = query.value(1).toString();
          data.title = query.value(2).toString();
          data.content = query.value(3).toString().replace("CHAR(13)","\n");
          list.push_back(data);
      }
      return list;
  }else{
      return list;
  }
}

